﻿/* *
 * =============================================================================
 * File Name:               Page.xaml.cs
 * Programmer:              Ken Watts
 * Creation Date:           11/10/2008
 * =============================================================================
 * Description:             Class that contains the drum pad implementation
 * =============================================================================
 * Modification Date:       11/10/2008 - Initial Revision
 * =============================================================================
 * */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Resources;

namespace DrumPad
{
    /// <summary>
    /// Quick hack to remove event handling that mark events as handled
    /// </summary>
    public class PadButton : System.Windows.Controls.Button
    {
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            e.Handled = false;
        }

        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonUp(e);
            e.Handled = false;
        }
    }

    public partial class Page : UserControl
    {
        StreamResourceInfo sriKick, sriSnare, sriHhc, sriHho, sriCrash, sriTom1, sriTom2, sriTom3;

        public Page()
        {
            InitializeComponent();
            // Kick drum
            PadButton buttonKick = new PadButton();
            buttonKick.Margin = new Thickness(10, 90, 310, 10);
            buttonKick.Content = "Kick (Z)";
            buttonKick.MouseLeftButtonDown += new MouseButtonEventHandler(buttonKick_MouseLeftButtonDown);
            LayoutRoot.Children.Add(buttonKick);
            sriKick = Application.GetResourceStream(new Uri("DrumPad;component/sounds/kick.mp3", System.UriKind.Relative));
            // Snare drum
            PadButton buttonSnare = new PadButton();
            buttonSnare.Margin = new Thickness(110, 90, 210, 10);
            buttonSnare.Content = "Snare (X)";
            buttonSnare.MouseLeftButtonDown += new MouseButtonEventHandler(buttonSnare_MouseLeftButtonDown);
            LayoutRoot.Children.Add(buttonSnare);
            sriSnare = Application.GetResourceStream(new Uri("/DrumPad;component/sounds/snare.mp3", System.UriKind.Relative));
            // Crash cymbal
            PadButton buttonCrash = new PadButton();
            buttonCrash.Margin = new Thickness(310, 10, 10, 90);
            buttonCrash.Content = "Crash (C)";
            buttonCrash.MouseLeftButtonDown += new MouseButtonEventHandler(buttonCrash_MouseLeftButtonDown);
            LayoutRoot.Children.Add(buttonCrash);
            sriCrash = Application.GetResourceStream(new Uri("DrumPad;component/sounds/crash.mp3", System.UriKind.Relative));
            // Tom 1
            PadButton buttonTom1 = new PadButton();
            buttonTom1.Margin = new Thickness(10, 10, 310, 90);
            buttonTom1.Content = "Tom 1 (A)";
            buttonTom1.MouseLeftButtonDown += new MouseButtonEventHandler(buttonTom1_MouseLeftButtonDown);
            LayoutRoot.Children.Add(buttonTom1);
            sriTom1 = Application.GetResourceStream(new Uri("DrumPad;component/sounds/tom1.mp3", System.UriKind.Relative));
            // Tom 2
            PadButton buttonTom2 = new PadButton();
            buttonTom2.Margin = new Thickness(110, 10, 210, 90);
            buttonTom2.Content = "Tom 2 (S)";
            buttonTom2.MouseLeftButtonDown += new MouseButtonEventHandler(buttonTom2_MouseLeftButtonDown);
            LayoutRoot.Children.Add(buttonTom2);
            sriTom2 = Application.GetResourceStream(new Uri("DrumPad;component/sounds/tom2.mp3", System.UriKind.Relative));
            // Tom 3
            PadButton buttonTom3 = new PadButton();
            buttonTom3.Margin = new Thickness(210, 10, 110, 90);
            buttonTom3.Content = "Tom 3 (D)";
            buttonTom3.MouseLeftButtonDown += new MouseButtonEventHandler(buttonTom3_MouseLeftButtonDown);
            LayoutRoot.Children.Add(buttonTom3);
            sriTom3 = Application.GetResourceStream(new Uri("DrumPad;component/sounds/tom3.mp3", System.UriKind.Relative));
            // HiHat closed
            PadButton buttonHiHatClosed = new PadButton();
            buttonHiHatClosed.Margin = new Thickness(210, 90, 110, 10);
            buttonHiHatClosed.Content = "HiHat Closed\r\n       (M)";
            buttonHiHatClosed.MouseLeftButtonDown += new MouseButtonEventHandler(buttonHiHatClosed_MouseLeftButtonDown);
            LayoutRoot.Children.Add(buttonHiHatClosed);
            sriHhc = Application.GetResourceStream(new Uri("/DrumPad;component/sounds/hhclosed.mp3", System.UriKind.Relative));
            // HiHat open
            PadButton buttonHiHatOpen = new PadButton();
            buttonHiHatOpen.Margin = new Thickness(310, 90, 10, 10);
            buttonHiHatOpen.Content = "HiHat Open\r\n      (K)";
            buttonHiHatOpen.HorizontalContentAlignment = HorizontalAlignment.Center;
            buttonHiHatOpen.MouseLeftButtonDown += new MouseButtonEventHandler(buttonHiHatOpen_MouseLeftButtonDown);
            LayoutRoot.Children.Add(buttonHiHatOpen);
            sriHho = Application.GetResourceStream(new Uri("DrumPad;component/sounds/hhopen.mp3", System.UriKind.Relative));

            // Handle keybpoard events
            this.KeyDown += new KeyEventHandler(Page_KeyDown);

        }

        void buttonHiHatOpen_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            meHiHat.SetSource(sriHho.Stream);
        }

        void buttonHiHatClosed_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            meHiHat.SetSource(sriHhc.Stream);
        }

        void buttonTom3_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            meTom3.SetSource(sriTom3.Stream);
        }

        void buttonTom2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            meTom2.SetSource(sriTom2.Stream);
        }

        void buttonTom1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            meTom1.SetSource(sriTom1.Stream);
        }

        void buttonCrash_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            meCrash.SetSource(sriCrash.Stream);
        }

        void buttonSnare_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            meSnare.SetSource(sriSnare.Stream);
        }

        private void buttonKick_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            meKick.SetSource(sriKick.Stream);
        }

        void Page_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Z:
                    buttonKick_MouseLeftButtonDown(sender, null);
                    break;
                case Key.X:
                    buttonSnare_MouseLeftButtonDown(sender, null);
                    break;
                case Key.C:
                    buttonCrash_MouseLeftButtonDown(sender, null);
                    break;
                case Key.A:
                    buttonTom1_MouseLeftButtonDown(sender, null);
                    break;
                case Key.S:
                    buttonTom2_MouseLeftButtonDown(sender, null);
                    break;
                case Key.D:
                    buttonTom3_MouseLeftButtonDown(sender, null);
                    break;
                case Key.M:
                    buttonHiHatClosed_MouseLeftButtonDown(sender, null);
                    break;
                case Key.K:
                    buttonHiHatOpen_MouseLeftButtonDown(sender, null);
                    break;
            }
        }
    }
}
